package wisCodeGener;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class Main {

	public static void main(String[] args) {

		String comName = "pxkglcom_fudanbcgn";
		String comCnName = "排选课管理";

		List<Po> poList = new ArrayList<Po>();
		poList.add(new Po("dsshxskb", "导师审核学生课表"));

		Writer out = null;
		try {
			Configuration configuration = new Configuration();
			configuration.setDirectoryForTemplateLoading(new File(Constants.template_path));

			Util.createDirectory(Constants.class_path);
			Util.createDirectory(Constants.class_path + "/po");
			Util.createDirectory(Constants.class_path + "/dao");
			Util.createDirectory(Constants.class_path + "/dao/impl");
			Util.createDirectory(Constants.class_path + "/service");
			Util.createDirectory(Constants.class_path + "/service/impl");
			Util.createDirectory(Constants.class_path + "/ws");

			for (Po po : poList) {
				Map<String, String> dataMap = new HashMap<String, String>();
				dataMap.put("comName", comName);
				dataMap.put("comCnName", comCnName);
				dataMap.put("name", po.getName());
				dataMap.put("display", po.getDisplay());
				dataMap.put("firstWordUpperName", po.getFirstWordUpperName());

				for (TplPo tpl : Constants.templateList) {
					Template template = configuration.getTemplate(tpl.getFilename());

					String finalFileName = Util.parseTemplate(tpl.getFinalFileName(), dataMap);
					File docFile = new File(Constants.class_path + "/" + tpl.getAppendPath() + "/" + finalFileName);
					out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
					template.process(dataMap, out);
				}
			}

			System.out.println("-- 生成完成！");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != out) {
					out.flush();
					out.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}

}
